<template>
    <el-dialog class="transparent-bg" v-model="visible" title="" width="20%" draggable>

        <div class="container">
      <h1>用户注册</h1>
      <div class="form">
        <div class="item">
          <label for="phoneNumID">手机号：</label><input class="input" v-model="phonenNum" autofocus size="mini" id="phoneNumID" style="width:'20%'" placeholder="请输入手机号">
                <!-- v-model把输入的值传输给name变量 -->
                <br/>
        </div>
        <div class="item">
            <label>密码：</label><input class="input" v-model="loginPass" autofocus id="loginPassID" :type="showPassword ? 'text' : 'password'" placeholder="请输入密码">
            <br/>
        </div>
        <div class="item">
            <label>确认密码：</label><input class="input" v-model="repeatLoginPass" autofocus id="repeatLoginPassID" :type="showPassword ? 'text' : 'password'" placeholder="请再次输入密码">
            <br/>
        </div>
        <div class="item">
            <label>昵称：</label><input class="input" v-model="loginName" autofocus size="mini" id="loginNameID" style="width:'20%'" placeholder="请输入昵称">
            <br/>
        </div>
      </div>
      
    </div>

        <template #footer>
            <span class="dialog-footer">
                <el-button class="button" @click="visible = false">取消</el-button>
                <el-button class="button" type="primary" @click="onConfirm">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
      
<script  setup>
import CryptoJS from 'crypto-js';
import { ref } from 'vue'
import { ElMessage } from 'element-plus';
import axios from 'axios';
import { useStore } from 'vuex';
const store = useStore();

const visible = ref(false)
const loginName = ref("游客")
const loginPass = ref("")
const phonenNum = ref("13631218234")
const repeatLoginPass = ref("")
var loginDlgParent = null;

const onConfirm = () => {
    console.log("login dlg show--------------:");

    if (loginPass.value.length < 6) {
        ElMessage('密码太弱');
        return;
    }

    if (repeatLoginPass.value != loginPass.value) {
        ElMessage('前后密码不一致！');
        return;
    }

    const hashedPassword = CryptoJS.SHA256(loginPass.value).toString();

    let data = {
        uname: loginName.value,
        password: hashedPassword,
        phoneNum: phonenNum.value
    };
    post2Login(data);
    visible.value = false;
}


var post2Login = function (data) {
    console.log("store login******************");
    var url = "api/user/reg";
    axios.post(url, data).then(res => {
        console.log(res.data);
        let rstObj = res.data;
        if (rstObj.ret == "success") {
            let user = {
                uid: rstObj.data.uid,
                name: rstObj.data.name,
                token: rstObj.data.token
            };
            store.commit("updateUser", user);
            if (loginDlgParent){
                loginDlgParent(true, rstObj.data.name);
            }
            ElMessage('登录成功');
        }
        else {
            ElMessage('注册失败：' + rstObj.errMsg);
        }
    }).catch(function (error) {
        console.log(error);
        ElMessage('登录失败');
    })
}

var setObserver = function(observer){
  loginDlgParent = observer;
}

defineExpose({
    visible,
    setObserver
});

</script>
    
<style>
#background{
    width: 100%;
    height: 100%;
    background-color: #4e5ef3;
    background-size:100% 100%;
    position: fixed;
    top: 0;
    left: 0;
}

.transparent-bg .el-dialog__body {
  /* 设置背景透明 */
  background: transparent;
  /* 设置内容透明度 */
  color: rgba(0, 0, 0, 0.7);
}

.container{
  width: 480px;
  height: 420px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background:#414883;
  text-align: center;
  border-radius: 20px;
  margin-top: 10px;
}
.container h1{
  color: aliceblue;
  margin-left: 20px;
}
.item {
  color: white;
  margin-left: 15%;
  margin-top: 35px;
  font-size: 16px;
  text-align: left;
}
.item label{
  float:left;
  width: 5em;
  margin-right: 1em;
  text-align: justify;
  text-align-last: justify;
}
.input{
  margin-left: -5px;
  padding: 4px;
  border: solid 1px #4e5ef3;
  outline: 0;
  font: normal 13px/100% Verdana,Tahoma,sans-serif;
  width: 200px;
  height: 23px;
  background:#f1f1f190;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 8px;
}
.button{
  position: relative;
  height: 33px;
  width: 100px;
  background: rgba(35, 19, 252, 0.425);
  border-radius: 10px;
  margin-top: 228px;
  box-shadow: none;
  color: white;
  margin-left: 40px;
  margin-right: 10px;

}
.keep{
  color: white;
}
.keep input{
  width: 15px;
  height: 15px;
  margin-top: 7px;
  margin-left: 10px;
  margin-right: 10px;
}
</style>
      